{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 1. Java, Arrays, and Sorting\n", "\n", "Douglas Blank, Fall 2015, CS110: Introduction to Computing" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Table of Contents\n", "* [1. Java, Arrays, and Sorting](#1.-Java,-Arrays,-and-Sorting)\n", "\t* [1.1 Review](#1.1-Review)\n", "\t\t* [1.1.1 An example:](#1.1.1-An-example:)\n", "\t* [1.2 Java](#1.2-Java)\n", "\t\t* [1.2.1 Differences between Java and Processing?](#1.2.1-Differences-between-Java-and-Processing?)\n", "\t* [1.3 Arrays](#1.3-Arrays)\n", "\t\t* [1.3.1 Primitive types](#1.3.1-Primitive-types)\n", "\t\t* [1.3.2 Arrays and Functions](#1.3.2-Arrays-and-Functions)\n", "\t* [1.4 Sorting](#1.4-Sorting)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.1 Review" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can now:\n", "\n", "* **define new types** using the **class** keyword\n", "* **create new instances** of a type with the **new** keyword\n", "* **call methods** of instance by using the **dot**\n", "\n", "This whole philosophy is called **Object Oriented Programming** or OOP. *Type and object are largely synonyms.*\n", "\n", "* Objects are typically nouns\n", "* Methods (ie, functions in classes) are verbs\n", "* Properties values are adjectives\n", "* `this` is a pronoun that means me" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.1.1 An example:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Imagine the alien creature Zoog from http://www.openprocessing.org/sketch/12199:\n", "\n", "```java\n", "class Zoog {\n", " Zoog(float tempX, float tempY, float tempW, \n", " float tempH, float tempEyeSize, \n", " float tempBodyColor1, float tempBodyColor2, float tempBodyColor3) {\n", " // ...\n", " }\n", " void jiggle(float speed) {\n", " // ...\n", " }\n", " void display(color eyeColor) {\n", " // ...\n", " }\n", "}\n", "```" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "format": "tab" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Created file '/home/dblank/public_html/CS110 Intro to Computing/2017-Spring/Lectures/Zoog.java'.\n" ] } ], "source": [ "%%file Zoog.java\n", "\n", "class Zoog {\n", " // Zoog's variables\n", " float x,y,w,h,eyeSize,bodyColor1,bodyColor2,bodyColor3;\n", " \n", " // Zoog constructor\n", " Zoog (float x, float y, float tempW, float tempH, float tempEyeSize, float tempBodyColor1, float tempBodyColor2, float tempBodyColor3) {\n", " this.x = x;\n", " this.y = y;\n", " this.w = tempW;\n", " this.h = tempH;\n", " this.eyeSize = tempEyeSize;\n", " this.bodyColor1 = tempBodyColor1;\n", " this.bodyColor2 = tempBodyColor2;\n", " this.bodyColor3 = tempBodyColor3;\n", " }\n", " \n", " // Move Zoog\n", " void jiggle(float speed) {\n", " // Change the location of Zoog randomly\n", " this.x = this.x + random(-1, 1) * speed;\n", " \n", " // Constrain Zoog to window\n", " this.x = constrain(this.x, 0, width - 2);\n", " this.y = constrain(this.y, 0, height - 2);\n", " }\n", " \n", " // Display Zoog\n", " void display(color eyeColor) {\n", " // Set ellipses and rects to CENTER mode\n", " ellipseMode(CENTER);\n", " rectMode(CENTER);\n", " \n", " // Draw Zoog's arms with a for loop\n", " for (float i = y - h/3; i < y + h/2; i += 10) {\n", " stroke(0);\n", " line(x-w/4,i,x+w/4,i);\n", " }\n", " \n", " // Draw Zoog's body\n", " stroke(0);\n", " fill(bodyColor1,bodyColor2,bodyColor3);\n", " rect(x,y,w/6,h);\n", " \n", " // Draw Zoog's head\n", " stroke(0);\n", " fill(255);\n", " ellipse(x,y-h,w,h);\n", " \n", " // Draw Zoog's eyes\n", " fill(eyeColor);\n", " ellipse(x-w/3,y-h,eyeSize,eyeSize*2);\n", " ellipse(x+w/3,y-h,eyeSize,eyeSize*2);\n", " \n", " // Draw Zoog's legs\n", " stroke(0);\n", " line(x-w/12,y+h/2,x-w/4,y+h/2+10);\n", " line(x+w/12,y+h/2,x+w/4,y+h/2+10);\n", " }\n", "}\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "\n", " var component = document.getElementById(\"sketch_1\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"state_1\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"controls_div_1\");\n", " if (component != undefined)\n", " component.remove();\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " // FIXME: Stop all previously running versions (?)\n", " var processingInstance = Processing.getInstanceById(\"canvas_1\");\n", " if (processingInstance != undefined && processingInstance.isRunning())\n", " processingInstance.noLoop();\n", " });\n", "\n", "\n", " var output_area = this;\n", " // find my cell element\n", " var cell_element = output_area.element.parents('.cell');\n", " // which cell is it?\n", " var cell_idx = Jupyter.notebook.get_cell_elements().index(cell_element);\n", " // get the cell object\n", " var cell = Jupyter.notebook.get_cell(cell_idx);\n", "\n", " function jyp_print(cell, newline) {\n", " return function(message) {\n", " cell.get_callbacks().iopub.output({header: {\"msg_type\": \"stream\"},\n", " content: {text: message + newline,\n", " name: \"stdout\"}});\n", " }\n", " }\n", " window.jyp_println = jyp_print(cell, \"\\n\");\n", " window.jyp_print = jyp_print(cell, \"\");\n", "\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " Processing.logger.println = jyp_print(cell, \"\\n\");\n", " Processing.logger.print = jyp_print(cell, \"\");\n", " });\n", "\n", "\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "
\n", " Sketch #1:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "
\n", "Sketch #1 state: Loading...
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%include Zoog.java\n", "\n", "// x, y, width, height, eye size, r, g, b:\n", "Zoog zoog1 = new Zoog(20, 70, 30, 10, 5, 128, 0, 0);\n", "Zoog zoog2 = new Zoog(50, 70, 30, 20, 5, 0, 128, 0);\n", "Zoog zoog3 = new Zoog(80, 70, 30, 30, 5, 0, 0, 128);\n", "\n", "void setup() {\n", " // eye color:\n", " zoog1.display(color(128, 0, 0));\n", " zoog2.display(color(255, 0, 0));\n", " zoog3.display(color(128, 0, 0));\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.2 Java" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The truth about Java:\n", "\n", "1. All code must be in a class, in a file\n", "2. Code must be \"compiled\"\n", "3. There is a special method called `main` whose type is `public static void`\n", " * public, because it can be seen from outside the class\n", " * static, because it doesn't require an \"instance\"\n", " * void, as before, it doesn't return anything\n", "\n", "In Jupyter, we can create a file in a number of ways. The easiest is to use the `%%file` magic:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Created file '/home/dblank/public_html/CS110 Intro to Computing/2017-Spring/Lectures/SomeClass.java'.\n" ] } ], "source": [ "%%file SomeClass.java\n", "\n", "public class SomeClass {\n", "\n", " public static void main(String[] args) {\n", " System.out.println(\"Hello, world!\");\n", " }\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then we compile the file using the Java Compiler, `javac`:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%%shell\n", " \n", "javac SomeClass.java" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finall, we can run the file using the Java Runtime called `java`:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello, world!\r\n", "\n" ] } ], "source": [ "%%shell\n", "\n", "java SomeClass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.2.1 Differences between Java and Processing?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.3 Arrays" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We saw that Java has arrays:\n", "\n", "We can:\n", "\n", "* define an array with `Type[] name`\n", "* create space for the array with `new Type[SIZE]`\n", "* create individual items with `new Type(...)`\n", "\n", "```java\n", "Zoog[] army;\n", "\n", "army = new Zoog[1000];\n", "\n", "for (int i; i < army.length; i++) {\n", " army[i] = new Zoog(...);\n", "}\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.3.1 Primitive types" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* int\n", "* float\n", "* bool\n", "\n", "With primitive types, you don't have to do the third step:\n", "\n", "```java\n", "int[] heights;\n", "\n", "heights = new int[100];\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.3.2 Arrays and Functions" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "\n", " var component = document.getElementById(\"sketch_2\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"state_2\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"controls_div_2\");\n", " if (component != undefined)\n", " component.remove();\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " // FIXME: Stop all previously running versions (?)\n", " var processingInstance = Processing.getInstanceById(\"canvas_2\");\n", " if (processingInstance != undefined && processingInstance.isRunning())\n", " processingInstance.noLoop();\n", " });\n", "\n", "\n", " var output_area = this;\n", " // find my cell element\n", " var cell_element = output_area.element.parents('.cell');\n", " // which cell is it?\n", " var cell_idx = Jupyter.notebook.get_cell_elements().index(cell_element);\n", " // get the cell object\n", " var cell = Jupyter.notebook.get_cell(cell_idx);\n", "\n", " function jyp_print(cell, newline) {\n", " return function(message) {\n", " cell.get_callbacks().iopub.output({header: {\"msg_type\": \"stream\"},\n", " content: {text: message + newline,\n", " name: \"stdout\"}});\n", " }\n", " }\n", " window.jyp_println = jyp_print(cell, \"\\n\");\n", " window.jyp_print = jyp_print(cell, \"\");\n", "\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " Processing.logger.println = jyp_print(cell, \"\\n\");\n", " Processing.logger.print = jyp_print(cell, \"\");\n", " });\n", "\n", "\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "
\n", " Sketch #2:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "
\n", "Sketch #2 state: Loading...
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "[46, 27, 27, 12, 38, 12, 28, 5, 40, 6, 0, 37, 1, 39, 34, 17, 19, 46, 1, 9, 3, 28, 32, 6, 32, 28, 41, 22, 33, 28, 13, 19, 24, 32, 18, 14, 43, 33, 4, 21, 23, 10, 26, 19, 40, 8, 25, 29, 10, 24, 19, 27, 8, 24, 26, 2, 42, 2, 46, 1, 4, 46, 32, 13, 14, 1, 0, 6, 45, 29, 42, 16, 26, 17, 14, 23, 11, 34, 1, 25, 33, 3, 22, 18, 16, 15, 23, 46, 46, 43, 49, 8, 27, 35, 22, 24, 0, 3, 42, 8, ]\n", "Biggest: 49 Smallest: 0\n" ] } ], "source": [ "int biggest(int[] myarray) {\n", " // what goes here?\n", " int biggest_so_far = -10000;\n", " for (int i = 0; i < myarray.length; i++) {\n", " if (myarray[i] > biggest_so_far) {\n", " biggest_so_far = myarray[i];\n", " }\n", " }\n", " return biggest_so_far;\n", "}\n", "\n", "int smallest(int[] myarray) {\n", " // what goes here?\n", " int smallest_so_far = 10000;\n", " for (int i = 0; i < myarray.length; i++) {\n", " if (myarray[i] < smallest_so_far) {\n", " smallest_so_far = myarray[i];\n", " }\n", " }\n", " return smallest_so_far;\n", "}\n", "\n", "void printArray(int[] myarray) {\n", " print(\"[\");\n", " for (int i = 0; i < myarray.length; i++) {\n", " print(myarray[i]);\n", " print(\", \");\n", " }\n", " println(\"]\");\n", "}\n", "\n", "int[] heights = new int[100];\n", "\n", "void setup() {\n", " for (int i = 0; i < 100; i++) {\n", " heights[i] = int(random(50));\n", " }\n", "\n", " int b = biggest(heights);\n", " int s = smallest(heights);\n", " \n", " printArray(heights);\n", "\n", " println(\"Biggest: \" + b + \" Smallest: \" + s);\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.4 Sorting" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "\n", " var component = document.getElementById(\"sketch_3\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"state_3\");\n", " if (component != undefined)\n", " component.remove();\n", " component = document.getElementById(\"controls_div_3\");\n", " if (component != undefined)\n", " component.remove();\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " // FIXME: Stop all previously running versions (?)\n", " var processingInstance = Processing.getInstanceById(\"canvas_3\");\n", " if (processingInstance != undefined && processingInstance.isRunning())\n", " processingInstance.noLoop();\n", " });\n", "\n", "\n", " var output_area = this;\n", " // find my cell element\n", " var cell_element = output_area.element.parents('.cell');\n", " // which cell is it?\n", " var cell_idx = Jupyter.notebook.get_cell_elements().index(cell_element);\n", " // get the cell object\n", " var cell = Jupyter.notebook.get_cell(cell_idx);\n", "\n", " function jyp_print(cell, newline) {\n", " return function(message) {\n", " cell.get_callbacks().iopub.output({header: {\"msg_type\": \"stream\"},\n", " content: {text: message + newline,\n", " name: \"stdout\"}});\n", " }\n", " }\n", " window.jyp_println = jyp_print(cell, \"\\n\");\n", " window.jyp_print = jyp_print(cell, \"\");\n", "\n", " require([window.location.protocol + \"//calysto.github.io/javascripts/processing/processing.js\"], function() {\n", " Processing.logger.println = jyp_print(cell, \"\\n\");\n", " Processing.logger.print = jyp_print(cell, \"\");\n", " });\n", "\n", "\n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "
\n", " Sketch #3:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "
\n", "Sketch #3 state: Loading...
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "[7, 10, 10, 6, 0, 7, 40, 7, 14, 32, 2, 32, 23, 44, 31, 25, 29, 10, 47, 32, 17, 12, 3, 28, 35, 48, 42, 17, 27, 46, 36, 39, 11, 43, 0, 27, 21, 40, 29, 25, 23, 6, 14, 5, 8, 25, 22, 16, 6, 7, 38, 28, 31, 10, 22, 6, 6, 39, 36, 37, 22, 2, 39, 39, 38, 29, 48, 29, 49, 47, 29, 36, 43, 42, 32, 46, 29, 23, 10, 5, 0, 5, 43, 26, 3, 22, 46, 4, 26, 35, 15, 18, 12, 18, 11, 32, 18, 4, 12, 42, ]\n", "[0, 0, 0, 2, 2, 3, 3, 4, 4, 5, 5, 5, 6, 6, 6, 6, 6, 7, 7, 7, 7, 8, 10, 10, 10, 10, 10, 11, 11, 12, 12, 12, 14, 14, 15, 16, 17, 17, 18, 18, 18, 21, 22, 22, 22, 22, 23, 23, 23, 25, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 29, 29, 29, 29, 31, 31, 32, 32, 32, 32, 32, 35, 35, 36, 36, 36, 37, 38, 38, 39, 39, 39, 39, 40, 40, 42, 42, 42, 43, 43, 43, 44, 46, 46, 46, 47, 47, 48, 48, 49, ]\n" ] } ], "source": [ "void sorted(int[] myarray) {\n", " // what goes here?\n", " for (int i = 0; i < myarray.length - 1; i++) {\n", " for (int j = i + 1; j < myarray.length; j++) {\n", " if (myarray[i] > myarray[j]) {\n", " int temp = myarray[i];\n", " myarray[i] = myarray[j];\n", " myarray[j] = temp;\n", " \n", " }\n", " } \n", " }\n", "}\n", "\n", "void printArray(int[] myarray) {\n", " print(\"[\");\n", " for (int i = 0; i < myarray.length; i++) {\n", " print(myarray[i]);\n", " print(\", \");\n", " }\n", " println(\"]\");\n", "}\n", "\n", "int[] heights = new int[100];\n", "\n", "void setup() {\n", " for (int i = 0; i < 100; i++) {\n", " heights[i] = int(random(50));\n", " }\n", " \n", " printArray(heights);\n", " sorted(heights);\n", " printArray(heights);\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Calysto Processing", "language": "java", "name": "calysto_processing" }, "language_info": { "codemirror_mode": { "name": "text/x-java", "version": 2 }, "file_extension": ".java", "mimetype": "text/x-java", "name": "java" } }, "nbformat": 4, "nbformat_minor": 0 }